package com.ctz.commonframe.ui.view;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by 陈堂正 on 2017/7/14.
 */

public class CountdownTextView extends TextView {
    private static final String TAG = CountdownTextView.class.getSimpleName();
    private static final int COUNT_DOWN_TICK = 1; //间隔

    public static final int MODE_SECOND_ONLY = 0;
    public static final int MODE_HOUR_MINUTE_SECOND = 1;

    private long mSeconds;
    private int mMode;
    private Map<Integer,Timer> mTimerMap;
    private TimerTask mTimerTask;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what){
                case COUNT_DOWN_TICK:
                    if(mSeconds <= 0){
                        if(mMode == MODE_SECOND_ONLY){
                            setText("0");
                        }else if(mMode == MODE_HOUR_MINUTE_SECOND){
                            setText("00:00:00");
                        }

                    }else {
                        if(mMode == MODE_SECOND_ONLY){
                            setText(mSeconds + "");
                        }else if(mMode == MODE_HOUR_MINUTE_SECOND){
                            setText(second2String(mSeconds));
                        }
                    }
                    break;
            }
        }
    };

    public CountdownTextView(Context context) {
        super(context);
    }

    public CountdownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CountdownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     *
     * @param mode
     * @param seconds
     */
    public void init(int mode,long seconds){
        mTimerMap = new HashMap<>();
        mMode = mode;
        mSeconds = seconds; //设置总共的秒数
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                if(mSeconds > 0){
                    mSeconds --;
                    mHandler.sendEmptyMessage(COUNT_DOWN_TICK);
                }
            }
        };
    }

    public void start(int position){
        if(mTimerMap.get(position) == null){
            Timer timer = new Timer();
            mTimerMap.put(position, timer);
            mTimerMap.get(position).schedule(mTimerTask, 0, 1000);
        }
    }

    /**
     * 转化为 hh:mm:ss 格式
     * @param second
     * @return
     */
    private String second2String(long second) {
        long hours = second / 3600;
        long minutes = (second % 3600)/ 60;
        long seconds = second % 60;

        String hourString = "";
        String minuteString = "";
        String secondString = "";
        if(hours < 10){
            hourString = "0";
            if(hours == 0){
                hourString += "0";
            }else{
                hourString += String.valueOf(hours);
            }
        }else{
            hourString = String.valueOf(hours);
        }
        if (minutes < 10) {
            minuteString = "0";
            if (minutes == 0) {
                minuteString += "0";
            }else {
                minuteString += String.valueOf(minutes);
            }
        }else{
            minuteString = String.valueOf(minutes);
        }
        if(seconds < 10){
            secondString = "0";
            if (seconds == 0) {
                secondString += "0";
            }else {
                secondString += String.valueOf(seconds);
            }
        }else {
            secondString = String.valueOf(seconds);
        }
        return hourString + ":" + minuteString+":"+secondString;
    }
}
